package com.chzc.fm.duotin.ui.channel;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.viewpager.CommFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/29.
 */
public class ListenRankActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvtitle;
    private ImageView ivback;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listenrank);
        initeViews();

        initEvents();

        initDatas();
    }

    public void initeViews()
    {
        tvtitle = (TextView) findViewById(R.id.listenrank_title_tv);
        ivback = (ImageView) findViewById(R.id.listenrank_back_iv);
        tabLayout = (TabLayout) findViewById(R.id.listenrank_tab_tl);
        viewPager = (ViewPager) findViewById(R.id.listenrank_content_vp);

        tabLayout.addTab(tabLayout.newTab().setText("今日"));
        tabLayout.addTab(tabLayout.newTab().setText("本周"));
        tabLayout.addTab(tabLayout.newTab().setText("本月"));
    }


    public void initEvents()
    {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                tabLayout.getTabAt(position).select();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        ivback.setOnClickListener(this);

    }


    public void initDatas()
    {
        List<Fragment> fragmentList = new ArrayList<>();

        fragmentList.add(new ListenTodayFragment());
        fragmentList.add(new ListenWeekFragent());
        fragmentList.add(new ListenMonthFragment());

        CommFragmentPagerAdapter fragmentPagerAdapter = new CommFragmentPagerAdapter(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(fragmentPagerAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.listenrank_back_iv:
                finish();
                break;
        }
    }
}
